package com.hanlink.dsp.util;

import android.graphics.RectF;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C) 秦智科技有限责任公司版权所有: 2015/10/11
 *
 * Description:
 *
 * Revision History
 * Date			         Author		  Description
 * 2015/10/11        fonhal       初始创建
 *
 * </pre>
 */
public class EqUtil {

    public static void cal_rect(RectF rectBounds, int left, float gain, int frency, int width, int height, int right, int top, int maxDb) {
        width = fc2int(width, 0, frency);
        height = (int) ((1.0F - gain / (float) maxDb) * (float) height / 2.0F);
        rectBounds.left = (float) (width + right - left);
        rectBounds.top = (float) (height - left + top);
        rectBounds.right = rectBounds.left + (float) (left * 2);
        rectBounds.bottom = rectBounds.top + (float) (left * 2);
    }


    public static final int fc2int(int width, int left, int frency) {
        return (int) ((Math.log10(frency) - Math.log10(20.0D)) / (Math.log10(22000.0D) - Math.log10(20.0D)) * (width - (double) left) + (double) left);
    }

    static double F = 48000.0D;
    static double R = 6.283185307179586D;

    public static void cal_point_peq(float q, float gain, int frency, double[] eqValue) {
        int length = eqValue.length;
        double var5 = Math.pow(10.0D, (double) (gain / 40.0F));
        double var9 = R * (double) frency / F;
        double var7 = Math.sin(var9) / (double) (2.0F * q);
        double var11;
        double var13;
        double var15;
        double var17;
        double var19;
        double var21;
        double var23;
        double var25;
        double var27;
        double var29;
        double var31;
        double var33;
        double var35;
        double var37;
        var11 = var7 / var5;
        var15 = -2.0D * Math.cos(var9);
        var13 = 1.0D - var7 / var5;
        var27 = -(2.0D * Math.cos(var9)) * 1.0D;
        var19 = (1.0D - var7 * var5) * 1.0D;

        for (int i = 0; i < length; ++i) {
            var37 = R * int2fc2(length, 0, i) / F;
            var33 = Math.cos(var37);
            var25 = Math.cos(2.0D * var37);
            var31 = -var27;
            var21 = Math.sin(var37);
            var35 = Math.sin(2.0D * var37);
            var23 = Math.cos(var37);
            var29 = Math.cos(2.0D * var37);
            var17 = -var15;
            var9 = Math.sin(var37);
            var37 = Math.sin(2.0D * var37);
            eqValue[i] = Math.log10(Math.sqrt((Math.pow(var33 * var27 + (1.0D + var7 * var5) * 1.0D + var25 * var19, 2.0D) + Math.pow(var31 * var21 - var35 * var19, 2.0D)) / (Math.pow(var23 * var15 + 1.0D + var11 + var29 * var13, 2.0D) + Math.pow(var17 * var9 - var37 * var13, 2.0D))));
        }

    }

    public static final double int2fc2(int var0, int var1, int var2) {
        double var3 = (double) var2;
        double var5 = (double) var0;
        return Math.pow(10.0D, (var3 - (double) var1) / (var5 - (double) var1) * (Math.log10(22000.0D) - Math.log10(20.0D)) + Math.log10(20.0D));
    }


    public static final void double2float(float times, int center, double[] doubleArray, int var3, float[] floatArray) {
        for (int i = 0; i < doubleArray.length; ++i) {
            floatArray[i * 4] = (float) (var3 + i);
            floatArray[i * 4 + 1] = (float) (doubleArray[i] * (double) times + (double) center);
        }
        int j;
        for (j = 0; j < doubleArray.length - 1; ++j) {
            floatArray[j * 4 + 2] = floatArray[(j + 1) * 4];
            floatArray[j * 4 + 3] = floatArray[(j + 1) * 4 + 1];
        }

        floatArray[j * 4 + 2] = floatArray[j * 4] + 1.0F;
        floatArray[j * 4 + 3] = floatArray[j * 4 + 1];
    }

}
